/*
 * Module dependencies
 */
var express = require('express')
  , stylus = require('stylus')
  , nib = require('nib')
  , feed = require('./feed')
  , speech = require('./speech')
  , render = require('./render')
  , upload = require('./upload')
  , passport = require('passport')
  , util = require('util')
  , GoogleStrategy = require('passport-google-oauth').OAuth2Strategy
  , auth = require('./auth')
  , BearerStrategy = require('passport-http-bearer').Strategy;


// Config passport strategies
// API Access link for creating client ID and secret:
// https://code.google.com/apis/console/
var GOOGLE_CLIENT_ID = "57234302156.apps.googleusercontent.com";
var GOOGLE_CLIENT_SECRET = "8vVsgau9xoHgPz3QJtP-FNJP"; 	

passport.use(new GoogleStrategy({
    clientID: GOOGLE_CLIENT_ID,
    clientSecret: GOOGLE_CLIENT_SECRET,
    callbackURL: 'http://www.10.0.0.7.xip.io:3000/auth/google/callback'
  },
  auth.googleVerifyCallback
));

passport.use(new BearerStrategy({
  },
  auth.bearerVerifyCallBack
));

var app = express()

function compile(str, path) {
  return stylus(str)
    .set('filename', path)
    .use(nib())
}

app.set('views', __dirname + '/views')
app.set('view engine', 'jade')

app.use(express.logger('dev'))
app.use(stylus.middleware(
  { src: __dirname + '/public'
  , compile: compile
  }
))
app.use(express.cookieParser());
app.use(express.json());
app.use(express.urlencoded());
app.use(require('connect-busboy')());
app.use(express.methodOverride());
app.use(passport.initialize());
app.use(app.router);

app.use(express.static(__dirname + '/public'))
app.use(express.static(__dirname + '/audio'))

app.get('/', 
  passport.authenticate(
    'bearer', 
    { failureRedirect: '/auth/failture', session: false }
  ),
  function(req, res) {
    console.log('route /');
    feed.getjson(req, res, render.desktop);
  }
)
app.get('/app/feed', 
  passport.authenticate(
    'bearer', 
    { failureRedirect: '/auth/failture', session: false }
  ),
  function(req, res) {
    console.log('route /app/feed');
    feed.getjson(req, res, render.app);
  }
)
app.get('/app/speech', 
  passport.authenticate(
    'bearer', 
    { failureRedirect: '/auth/failture', session: false }
  ),
  function(req, res) {
    console.log('route /app/speech');
    speech.getjson(req, res, render.app);
  }
)
app.get('/auth/google/logout',
  passport.authenticate(
    'bearer', 
    { failureRedirect: '/auth/failture', session: false }
  ),
  function(req, res) {
    console.log('route /auth/google/logout');
    auth.logout(req, res);
  }
);
app.post('/upload', 
  passport.authenticate(
    'bearer', 
    { failureRedirect: '/auth/failture', session: false }
  ),
  function(req, res) {
    console.log('route /upload');
    upload.execute(req, res);
  }
)
app.get('/auth/google/login',
  passport.authenticate(
    'google', 
    { scope: [
        'https://www.googleapis.com/auth/userinfo.profile',
        'https://www.googleapis.com/auth/userinfo.email'],
      failureRedirect: '/auth/failture', 
      session: false 
    }
  ),
  function(req, res){}
);
app.get('/auth/google/callback', 
  passport.authenticate(
    'google', 
    { failureRedirect: '/auth/failure', session: false }
  ),
  function(req, res) {
    console.log('/auth/google/callback');
    auth.sendToken(req, res);
  }
);
app.get('/auth/failure', function(req, res) {
  console.log('failed'); 
  auth.failure(req, res);
});

app.listen(3000)